package com.easyui.service.impl;

import com.easyui.dao.MenuDAO;
import com.easyui.dao.UserDAO;
import com.easyui.pojo.User;
import com.easyui.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;
    @Autowired
    private MenuDAO menuDAO;

    @Override
    public void doLogin(String name, String password) {
//        User user = userDAO.getUserByName(name);
//        if (user == null || !password.equals(user.getPassword())) {
//            return null;
//        }
        UsernamePasswordToken token = new UsernamePasswordToken(name, password);
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
    }

    @Override
    public void addUser(User user) {
        userDAO.addUser(user);
    }

    @Override
    public void deleteUser(Integer[] ids) {
        for (Integer id : ids) {
            userDAO.deleteUser(id);
            menuDAO.deleteUserMenu(id);
        }
    }

    @Override
    public void updateUser(User user) {
        userDAO.updateUser(user);
    }

    @Override
    public PageInfo<User> getUserList(User user, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize, "id desc");
        List<User> users = userDAO.getUserList(user);
        return new PageInfo<>(users);
    }

    @Override
    public User getUserById(Integer id) {
        return userDAO.getUserById(id);
    }

    @Override
    public User getUserByName(String name) {
        return userDAO.getUserByName(name);
    }
}
